<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/7/18
 * Time: 上午1:08
 */
namespace app\admin\controller;
use app\common\model\Settings;
use \think\Exception;
use think\Db;
class Index extends Common
{
    public function index()
    {   $this->templateTitle('首页');
        /*最近七天的时间*/
        $category=json_encode(
            array(
                array("label"=>date("Y-m-d",strtotime("-6 day"))),
                array("label"=>date("Y-m-d",strtotime("-5 day"))),
                array("label"=>date("Y-m-d",strtotime("-4 day"))),
                array("label"=>date("Y-m-d",strtotime("-3 day"))),
                array("label"=>date("Y-m-d",strtotime("-2 day"))),
                array("label"=>date("Y-m-d",strtotime("-1 day"))),
                array("label"=>date("Y-m-d")),
            )
        );

        /*获取会员数量*/
        $user=Db::name('user');
            $num=$user
                ->field('count(*) as num')
                ->find();
            $numtoday=$user
                ->where("create_time",">= time",date("Y-m-d"))
                ->field('count(*) as numtoday')
                ->find();
            $numyestoday=$user
                ->where("create_time",">= time",date("Y-m-d",strtotime("-1 day")))
                ->where("create_time","< time",date("Y-m-d"))
                ->field('count(*) as numyestoday')
                ->find();
            $num1=$user
                ->where("create_time","< time",date("Y-m-d"))
                ->field('count(*) as num1')
                ->find();
            $num2=$user
                ->where("create_time","< time",date("Y-m-d",strtotime("-1 day")))
                ->field('count(*) as num2')
                ->find();
            $num3=$user
                ->where("create_time","< time",date("Y-m-d",strtotime("-2 day")))
                ->field('count(*) as num3')
                ->find();
            $num4=$user
                ->where("create_time","< time",date("Y-m-d",strtotime("-3 day")))
                ->field('count(*) as num4')
                ->find();
            $num5=$user
                ->where("create_time","< time",date("Y-m-d",strtotime("-4 day")))
                ->field('count(*) as num5')
                ->find();
            $num6=$user
                ->where("create_time","< time",date("Y-m-d",strtotime("-5 day")))
                ->field('count(*) as num6')
                ->find();
            $num7=$user
                ->where('identity',2)
                ->count();


          $dataset=json_encode(array(
              array("value"=>$num6['num6']),
              array("value"=>$num5['num5']),
              array("value"=>$num4['num4']),
              array("value"=>$num3['num3']),
              array("value"=>$num2['num2']),
              array("value"=>$num1['num1']),
              array("value"=>$num['num']),
          ));


        $this->assign('allnum',$num['num']);
        $this->assign('numtoday',$numtoday['numtoday']);
        $this->assign('numyestoday',$numyestoday['numyestoday']);
        $this->assign('categories',$category);
        $this->assign('dataset',$dataset);
        $this->assign('num7',$num7);


        /*活动*/
        $time = time();
        $active=Db::name('active');
        /*所有活动*/
        $activeallcount=$active
            ->count();
        /*未开始*/
        $activeone=$active
            ->where('begintime','gt',$time)
            ->count();
        /*进行中*/
        $activetwo=$active
            ->where('begintime','elt',$time)
            ->where('endtime','egt',$time)
            ->count();
        /*已结束*/
        $activethree=$active
            ->where('endtime','lt',$time)
            ->count();
        /*待结算*/
        $activefour=$active
            ->where('status',0)
            ->count();
        /*完成*/
        $activefive=$active
            ->where('status',1)
            ->count();

        $alldata=json_encode(array(
            array(
                "label"=>"未开始",
                "value"=>empty($activeone)?0:$activeone,
            ),
            array(
                "label"=>"进行中",
                "value"=>empty($activetwo)?0:$activetwo,
            ),
            array(
                "label"=>"已结束",
                "value"=>empty($activethree)?0:$activethree,
            )
        ));
        $alldata2=json_encode(array(
            array(
                "label"=>"待结算",
                "value"=>$activefour,
            ),
            array(
                "label"=>"已结算",
                "value"=>$activefive,
            )
        ));
        $this->assign('alldata',$alldata);
        $this->assign('alldata2',$alldata2);
        $this->assign('activeallcount',$activeallcount);
        $this->assign('activeone',empty($activeone)?0:$activeone);
        $this->assign('activetwo',empty($activetwo)?0:$activetwo);
        $this->assign('activethree',empty($activethree)?0:$activethree);
        $this->assign('activefour',empty($activefour)?0:$activefour);
        $this->assign('activefive',empty($activefive)?0:$activefive);

        return $this->fetch();
    }

}